﻿using UnityEngine;
using System.Collections;

public class ShuXing : MonoBehaviour {
    //武器装备格子
    public GameObject wuqi;
    //盔甲装备格子
    public GameObject kuijia;
    //手镯装备格子
    public GameObject shouzhuo;
    //水晶装备格子
    public GameObject shuijing;    
    //存储武器格子的武器信息
    private Item wuqide;
    //盔甲的
    private Item kuijiade;
    //手镯的
    private Item shouzhuode;
    //水晶的
    private Item shuijingde;
    //hp属性栏
    public GameObject hp;
    //sp属性栏
    public GameObject sp;
    //att属性栏
    public GameObject att;
    //def属性栏
    public GameObject def;
    //武器格子标志位
    private int Swit1 = 0;
    //盔甲格子标志位
    private int Swit2 = 0;
    //手镯格子标志位
    private int Swit3 = 0;
    //水晶格子标志位
    private int Swit4 = 0;
	
	void Update () {
        //武器装备栏的判断
        if (wuqi.transform.childCount > 0)
        {
            if (Swit1 < 1)
            {
                DragItem dragItem = wuqi.GetComponentInChildren<DragItem>();
                setAttribute(dragItem.ThisItem, true);
                wuqide = dragItem.ThisItem;
                Swit1 = Swit1 + 1;
            }
        }
        else
        {
            if (Swit1 > 0)
            {
                setAttribute(wuqide, false);
                wuqide = null;
                Swit1 = Swit1 - 1;
            }
        }
        //盔甲装备栏的判断
        if (kuijia.transform.childCount > 0)
        {
            if (Swit2 < 1)
            {
                DragItem dragItem = kuijia.GetComponentInChildren<DragItem>();
                setAttribute(dragItem.ThisItem, true);
                kuijiade = dragItem.ThisItem;
                Swit2 = Swit2 + 1;
            } 
        }
        else
        {
            if (Swit2 > 0)
            {
                setAttribute(kuijiade, false);
                kuijiade = null;
                Swit2 = Swit2 - 1;
            }           
        }
        //手镯装备栏的判断
        if (shouzhuo.transform.childCount > 0)
        {
            if (Swit3 < 1)
            {
                DragItem dragItem = shouzhuo.GetComponentInChildren<DragItem>();
                setAttribute(dragItem.ThisItem, true);
                shouzhuode = dragItem.ThisItem;
                Swit3 = Swit3 + 1;
            }
        }
        else
        {
            if (Swit3 > 0)
            {
                setAttribute(shouzhuode, false);
                shouzhuode = null;
                Swit3 = Swit3 - 1;
            }
        }
        //水晶装备栏的判断
        if (shuijing.transform.childCount > 0)
        {
            if (Swit4 < 1)
            {
                DragItem dragItem = shuijing.GetComponentInChildren<DragItem>();
                setAttribute(dragItem.ThisItem, true);
                shuijingde = dragItem.ThisItem;
                Swit4 = Swit4 + 1;
            }
        }
        else
        {
            if (Swit4 > 0)
            {
                setAttribute(shuijingde, false);
                shuijingde = null;
                Swit4 = Swit4 - 1;
            }
        }        
	}
    //有添加装备后修改属性方法
    public void setAttribute(Item dragItem, bool isNull)
    {
        if (isNull)
        {
            UILabel attL = att.GetComponentInChildren<UILabel>();
            UILabel defL = def.GetComponentInChildren<UILabel>();
            UILabel hpL = hp.GetComponentInChildren<UILabel>();
            UILabel spL = sp.GetComponentInChildren<UILabel>();

            int a = int.Parse(attL.text);
            int d = int.Parse(defL.text);
            int h = int.Parse(hpL.text);
            int s = int.Parse(spL.text);

            a += dragItem.Att;
            d += dragItem.Def;
            h += dragItem.Hp;
            s += dragItem.Sp;

            attL.text = a + "";
            defL.text = d + "";
            hpL.text = h + "";
            spL.text = s + "";
        }
        else
        {
            UILabel attL = att.GetComponentInChildren<UILabel>();
            UILabel defL = def.GetComponentInChildren<UILabel>();
            UILabel hpL = hp.GetComponentInChildren<UILabel>();
            UILabel spL = sp.GetComponentInChildren<UILabel>();

            int a = int.Parse(attL.text);
            int d = int.Parse(defL.text);
            int h = int.Parse(hpL.text);
            int s = int.Parse(spL.text);

            a -= dragItem.Att;
            d -= dragItem.Def;
            h -= dragItem.Hp;
            s -= dragItem.Sp;

            attL.text = a + "";
            defL.text = d + "";
            hpL.text = h + "";
            spL.text = s + "";
        } 
    }
}
